package com.hs.wa.redis;


import com.hs.wa.utls.RedisCacheOfMysql;
import com.springnew.dao.PersonMapper;
import com.springnew.dao.RuleconfigMapper;
import com.springnew.model.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MysqlAndRedis {
    @Autowired
    private RedisCacheOfMysql redisCacheOfMysql;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private RuleconfigMapper ruleconfigMapper;


   @Test
    public void redisOfMySQL() {
        String v = "1332854729@qq.com";
        List<Person> userList = (List<Person>) redisCacheOfMysql.selectTableOfMysql(Person.class, "username", v, PersonKey.class);
//        redisCacheOfMysql.selectTable(User.class, "name", "zhangsan", UserKey.class);
        System.out.println(userList);
        for (Person user : userList) {
            if (user.getUsername().equals(v)) {
                System.out.println("最外层获取User 对象成功" + user);
            }

        }
    }

//    @Test
//    public void redisOfMySQLNoPraryKey() {
//        String v = "zhansan";
//        List<Person> userList = redisCacheOfMysql.selectMysqlNoPrimaryKey(Person.class, "username", v);
////        redisCacheOfMysql.selectTable(User.class, "name", "zhangsan", UserKey.class);
//        System.out.println(userList);
//        for (Person user : userList) {
//            if (user.getUsername().equals(v)) {
//                System.out.println("最外层获取User 对象成功" + user);
//            }
//
//        }
//    }


    @Test
    public void redisOfMySQLForHashKey() {
        String v = "haha";
        List<Person> userList = redisCacheOfMysql.selectMysqlForHashKey(Person.class, "nickname", v);
//        redisCacheOfMysql.selectTable(User.class, "name", "zhangsan", UserKey.class);
        for (Person user : userList) {
            if (user.getNickname().equals(v)) {
                System.out.println("最外层获取User 对象成功" + user);
            }

        }
    }

    @Test
    public void redisOfMySQLForJdbcNoMap() {
        String v = "root";
        List<Person> userList = redisCacheOfMysql.selectMysqlForJdbcNoMap(Person.class, "username", v);
//        redisCacheOfMysql.selectTable(User.class, "name", "zhangsan", UserKey.class);
        for (Person user : userList) {
            if (user.getNickname().equals(v)) {
                System.out.println("最外层获取User 对象成功" + user);
            }

        }
    }

    /**
     * 查询数据库所有表
     */
    @Test
    public void jdbc_show_tables() {
        String sqlstr = "show tables";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sqlstr);

        for (Map<String, Object> map : maps) {


            for (String key : map.keySet()) {
                System.out.println("key= " + key + " and value= " + map.get(key));
            }

        }

    }

    /**
     * 查询数据库所有表
     */
    @Test
    public void jdbc_select_config() {
        String sqlstr = "show tables";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sqlstr);
        for (Map<String, Object> map : maps) {
            for (String key : map.keySet()) {
                //   System.out.println("key= " + key + " and value= " + map.get(key));
                System.out.println(map.get(key));
            }
        }
    }

    /**
     *
     */
    @Test
    public void jdbc_select_mapper() {
        PersonExample userCondition = new PersonExample();
        String name = "root";
        userCondition.createCriteria().andUsernameEqualTo(name);
        List<Person> userList = personMapper.selectByExample(userCondition);
        System.out.println(userList);

    }

    @Test
    public void testHash() {
        int a = 1;
        RuleconfigExample ruleconfigCondition = new RuleconfigExample();
        List<Ruleconfig> ruleconfigs;
        if (a == 1) {
            ruleconfigCondition.createCriteria().andStatusEqualTo("1");
            ruleconfigs = ruleconfigMapper.selectByExample(ruleconfigCondition);
        } else {
            ruleconfigs = redisCacheOfMysql.selectMysqlForJdbcNoMap(Ruleconfig.class, "status", "1");
        }
        System.out.println(ruleconfigs);
    }

    @Test
    public void testHash1() {
        int a = 1;
        RuleconfigExample ruleconfigCondition = new RuleconfigExample();
        List<Ruleconfig> ruleconfigs;
        if (a == 1) {
            ruleconfigCondition.createCriteria().andStatusEqualTo("1");
            ruleconfigs = ruleconfigMapper.selectByExample(ruleconfigCondition);
        } else {
            ruleconfigs = redisCacheOfMysql.selectMysqlForJdbcNoMap(Ruleconfig.class, "status", "1");
        }
        System.out.println(ruleconfigs);
    }
}
